package cn.yaboya.system.controller;



import cn.yaboya.basic.utils.AjaxResult;
import cn.yaboya.basic.utils.PageList;
import cn.yaboya.system.domain.Dictionary;
import cn.yaboya.system.domain.Dictionarydetail;
import cn.yaboya.system.query.DictionaryQuery;
import cn.yaboya.system.query.DictionarydetailQuery;
import cn.yaboya.system.service.IDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数字字典的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/dic")
public class DictionaryController {

    @Autowired
    private IDictionaryService dictionaryService;

    /**
     * @Title: loadById
     * @Description: 根据一个id查询单个
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/6/24 10:05
     * @Parameters: [id]
     * @Return cn.yaboya.org.domain.Dictionary
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "id查找" )
    public Dictionary loadById(@PathVariable("id") Long id){
        return dictionaryService.loadById(id);
    }

    /**
     * @Title: loadAll
     * @Description: 查询全部
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/6/24 10:04
     * @Parameters: []
     * @Return java.util.List<cn.yaboya.org.domain.Dictionary>
     */
    @GetMapping
    @ApiOperation(value = "查找全部" )
    public List<Dictionary> loadAll(){
        return dictionaryService.loadAll();
    }

    /**
     * @Title:remove
     * @Description: 删除单个
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/6/24 10:06
     * @Parameters:
     * @Return
     */
    @PostMapping("/byId")
    public PageList<Dictionarydetail> loadByIdType(@RequestBody DictionarydetailQuery dictionarydetailQuery){
        PageList<Dictionarydetail> data = dictionaryService.loadByIdType(dictionarydetailQuery);
        return data;
    }


    @DeleteMapping("{id}")
    @ApiOperation(value = "id删除" )
    public AjaxResult remove(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            dictionaryService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试");
        }
    }
    /**
     * @Title: addOrUpt
     * @Description: 增加 或者修改接口
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/6/24 12:41
     * @Parameters: [employee]
     * @Return cn.yaboya.basic.utils.AjaxResult
     */
    @ApiOperation(value = "新增或者修改" )
    @PutMapping
    public AjaxResult addOrUpt(@ApiParam(name="json 对象",required = true) @RequestBody Dictionary dictionary){
        try {
            // 判断是否有id 没有就新增 有就更改
            if (dictionary.getId() == null){
                dictionaryService.add(dictionary);
            } else {
                dictionaryService.update(dictionary);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试");
        }
    }

    /**
     * @Title: queryData
     * @Description: 分页+高级查询
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/24 9:51
     * @Parameters: [employeeQuery]
     * @Return cn.itsource.basic.utils.AjaxResult
     */
    @PostMapping
    public PageList<Dictionary> queryData(@RequestBody DictionaryQuery dictionaryQuery){
        System.out.println(dictionaryQuery);
        PageList<Dictionary> data = dictionaryService.queryData(dictionaryQuery);
        return data;
    }

    /**
     * @Title: batchRem
     * @Description: 批量删除
     * @Author: LT
     * @Version: 1.0
     * @Date:  2022/6/25 11:28
     * @Parameters: [ids]
     * @Return cn.yaboya.basic.utils.AjaxResult
     */
    @PatchMapping
    public AjaxResult batchRem(@RequestBody Long[] ids){
        try {
            dictionaryService.batchRem(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后再试");
        }
    }

}
